package com.dtb.trade.service;

import com.alibaba.fastjson.JSON;
import com.dtb.trade.dao.mysql.TradeDao;
import com.dtb.trade.entity.Block;
import com.dtb.trade.entity.PointDeal;
import com.dtb.trade.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:ChengJian
 * @Description:
 * @Date: Created in 上午11:27 2018/8/8
 */
@Service
public class BlockUtil {

    @Autowired
    TradeDao tradeDao;

    //交易记账，加入系统区块交易记录
    public void tally(Block block, List<PointDeal> pointDeals){


        int blockNum   = 0;

        if (block.getPointDeals() != null){
            blockNum   = block.getPointDeals().size();
        }

        //该区块已经拥有的交易记录个数


        //准备记账的记忆记录个数
        int recordNum  = pointDeals.size();

        //区块拥有交易记录的最大个数
        int blockMax   =5;

        //欠缺，补足到最大区块个数
        int deficiency = blockMax - blockNum;

        //该区块是否能满足记账交易记录的个数，0刚好满足，正数绰绰有余，负数不满足，生成新的区块
        int surplusNum = deficiency-recordNum;


        if (blockNum < blockMax){
                if (surplusNum >= 0){
                    block.getPointDeals().addAll(pointDeals);
                    block.setData(JSON.toJSONString(block.getPointDeals()));
                    tradeDao.insertBlock(block);
                }else {
                    //填补区块剩余
                    for (int j =0;j<deficiency;j++){
                        block.getPointDeals().add(pointDeals.get(j));
                        pointDeals.remove(j);
                        //更新到数据库
                        tradeDao.updateBlock(block);
                    }

                    //生成新区块
                    int nonce      = block.getNonce();
                    int index      = block.getIndex()+1;
                    //String time  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    String preHash = block.getHash();
                    long timestamp = System.currentTimeMillis();
                    String data    = JSON.toJSONString(block.getPointDeals());
                    String hash    = EncryptUtil.Encrypt(preHash+data+nonce+timestamp+block.getIndex());

                    if (hash.startsWith("0000")){
                        System.out.println("生成新的区块");
                        Block newBlock = new Block(index,timestamp,pointDeals,data,nonce,preHash,hash);
                        //添加新增区块到数据库
                        tradeDao.insertBlock(block);
                    }else {
                        System.out.println("发生错误");
                    }
                }
        }else {
            //直接生成新区块
            int nonce      = block.getNonce();
            int index      = block.getIndex()+1;
            //String time  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            String preHash = block.getHash();
            long timestamp = System.currentTimeMillis();
            String data    = JSON.toJSONString(block.getPointDeals());
            String hash    = EncryptUtil.Encrypt(preHash+data+nonce+timestamp+block.getIndex());
            if (hash.startsWith("0000")){
                Block newBlock = new Block(index,timestamp,pointDeals,data,nonce,preHash,hash);
                //添加新增区块到数据库
                tradeDao.insertBlock(block);
            }else {
                System.out.println("发生错误");
            }
        }
    }

}
